package io.github.jianjianghui.groot.core;

import io.github.jianjianghui.groot.core.annotation.ContentFormatValidator;

import java.util.Objects;
import java.util.StringJoiner;

/**
 * 幂等请求
 *
 * @author <a href="https://www.github.com/jianjianghui">Jianghui Jian<a/>
 * @date 2021/8/27 - 13:06
 */
public class GrootRequest {
    String requestId;
    String ip;
    GrootStatusEnum statusEnum;

    public GrootRequest() {
    }

    public GrootRequest(String requestId, String ip) {
        this.requestId = requestId;
        this.ip = ip;
    }

    /**
     * 创建幂等请求
     *
     * @param ip        ip
     * @param requestId requestId
     * @return {@link GrootRequest}
     */
    public static GrootRequest create(String ip, String requestId, ContentFormatValidator contentFormatValidator) {
        if (contentFormatValidator.valid(requestId)) {
            return null;
        }

        return new GrootRequest(requestId, ip);
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public GrootStatusEnum getStatusEnum() {
        return statusEnum;
    }

    public void setStatusEnum(GrootStatusEnum statusEnum) {
        this.statusEnum = statusEnum;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        GrootRequest that = (GrootRequest) o;
        return Objects.equals(requestId, that.requestId) && Objects.equals(ip, that.ip);
    }

    @Override
    public int hashCode() {
        return Objects.hash(requestId, ip);
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", GrootRequest.class.getSimpleName() + "[", "]")
                .add("requestId='" + requestId + "'")
                .add("ip='" + ip + "'")
                .add("statusEnum=" + statusEnum)
                .toString();
    }
}
